﻿#region Usings

using System;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Threading;
using ScrumTable.BL.DM.DataManagement;
using ScrumTable.Common.Unity;
using ScrumTable.Config.ElementManagement;
using ScrumTable.UI.ViewModel;

#endregion

namespace ScrumTable.UI.View.CustomControl
{
    /// <summary>
    /// StateIcon
    /// </summary>
    public class StateIcon : UserControl
    {

        #region Declarations

        private static ElementManager _elementManager;
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private static ElementManager ElementManager
        {
            get
            {
                if(_elementManager == null)
                {
                    _elementManager =  (((IAppScreen)Application.Current.MainWindow)).Container.Resolve<ElementManager>();
                }
                return _elementManager;
            }
        }

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------
        
   
        /// <summary>
        /// Property for State
        /// </summary>
        public static readonly DependencyProperty StateProperty =
            DependencyProperty.Register("State",
                                        typeof(ScrumState),
                                        typeof(StateIcon),
                                        new UIPropertyMetadata(null, OnStateChanged));
        /// <summary>
        /// Gets or sets if the state for this control
        /// </summary>
        /// <value>If items are removed after drag complete.</value>
        public ScrumState State
        {
            get { return (ScrumState)GetValue(StateProperty); }
            set { SetValue(StateProperty, value); }
        }

        /// <summary>
        /// Background icon id
        /// </summary>
        public string Icon { get; set; }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------
 
        #endregion


     

        #region Events

        private static void OnStateChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
             
            
            var iconControl = ((StateIcon) (sender));
            if (iconControl != null && iconControl.State != null)
            {

                string ressource = ElementManager.GetImgRessourceString(iconControl.State);
                if (ressource != null)
                {
                    int size = 14;
                    iconControl.Content = new Label
                                              {
                                                  Background = Application.Current.Resources[ressource] as Brush,
                                                  MaxHeight = size,
                                                  MaxWidth = size,
                                                  MinWidth = size,
                                                  MinHeight = size,
                                                  HorizontalAlignment = System.Windows.HorizontalAlignment.Center,
                                                  VerticalAlignment = System.Windows.VerticalAlignment.Center
                                              };
                }
                iconControl.Background = Application.Current.Resources[iconControl.Icon] as Brush;
            }
        }
        #endregion
    }
}
